package com.woniu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.Reservation;
import com.woniu.service.ReservationService;
import com.woniu.mapper.ReservationMapper;
import com.woniu.vo.ReservationVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Apricity
* @description 针对表【reservation(预约表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:59
*/
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation>
    implements ReservationService{

    @Resource
    private ReservationMapper reservationMapper;

    @Override
    public List<ReservationVO> getReservationList(String keyword, Integer pageNum, Integer pageSize) {
        Page<ReservationVO> page = new Page<>(pageNum, pageSize);
        List<ReservationVO> reservations;
        
        if (keyword != null && !keyword.trim().isEmpty()) {
            reservations = reservationMapper.queryReservationListByKeyword(keyword, page);
        } else {
            reservations = reservationMapper.queryReservationList(page);
        }
        
        return reservations;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addReservation(Reservation reservation) {
        // 设置预约状态为待充电(1)
        reservation.setStatus(1);
        
        return reservationMapper.insert(reservation) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteReservation(Long id) {
        return reservationMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateReservation(Reservation reservation) {
        if (reservation.getId() == null) {
            return false;
        }
        return reservationMapper.updateReservation(reservation) > 0;
    }
}




